package 笔试;

import java.util.Scanner;

public class 好未来_岛屿计算 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int row = scan.nextInt();
        int col = scan.nextInt();
        int[][] glad = new int[row][col];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                glad[i][j] = scan.nextInt();
            }
            scan.nextLine();
        }
        int res = numIslands(glad);
        System.out.println(res);
    }

    public static int[][] nextP= {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    public static int numIslands(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int count = 0;
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(grid[i][j] == 1) {
                    count++;
                    dfs(grid, i, j);
                }
            }
        }
        return count;
    }

    public static void dfs(int[][] grid, int i, int j) {
        grid[i][j] = 2;

        for(int z = 0; z < 4; z++) {
            int nI = i + nextP[z][0];
            int nJ = j + nextP[z][1];
            if(nI < 0 || nI >= grid.length || nJ < 0 || nJ >= grid[0].length) {
                continue;
            }
            if(grid[nI][nJ] == 1) {
                dfs(grid, nI, nJ);
            }

        }
    }
}
